package com.aisi.backend.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.aisi.backend.domain.entity.Account;
import com.aisi.backend.domain.vo.request.LoginAccountVo;
import com.aisi.backend.domain.vo.request.RegisterAccountVo;
import com.aisi.backend.domain.vo.request.ResetAccountVo;
import com.aisi.backend.domain.vo.response.AccountDetailsVo;
import com.aisi.backend.domain.vo.response.AccountVo;
import com.aisi.backend.domain.vo.response.LoginResponseVo;
import com.aisi.backend.mapper.AccountDetailsMapper;
import com.aisi.backend.mapper.AccountMapper;
import com.aisi.backend.service.AccountService;
import com.aisi.backend.utils.JwtUtils;
import com.aisi.backend.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Optional;

/**
 * @Author: shenjianZ
 * @Date: 2024/5/1 上午11:49
 * @Description:
 */


public class AccountServiceImpl implements AccountService {


    // 定义常量来保存盐值
    private static final String SALT = "$2a$10$xhN3llT/./DyTwUQjgTTRe";

    @Override
    public Integer register(RegisterAccountVo registerAccountVo) {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            AccountMapper mapper = session.getMapper(AccountMapper.class);
            String encodePassword = BCrypt.hashpw(registerAccountVo.getPassword(), SALT);
            registerAccountVo.setPassword(encodePassword);
            Account account = new Account();
            account.setUsername(registerAccountVo.getUsername());
            account.setPassword(registerAccountVo.getPassword());
            Integer register = mapper.register(account);
            Account account1 = mapper.findAccount(new LoginAccountVo(registerAccountVo.getUsername(), registerAccountVo.getPassword()));
            initAccountDetails(account1.getId());
            return register;
        }
    }

    public  void initAccountDetails(Integer userId ) {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            AccountDetailsMapper mapper = session.getMapper(AccountDetailsMapper.class);
            mapper.initAccountDetails(userId);
        }
    }
    @Override
    public LoginResponseVo login(LoginAccountVo loginAccountVo) {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            AccountMapper mapper = session.getMapper(AccountMapper.class);
            String encodePassword = BCrypt.hashpw(loginAccountVo.getPassword(), SALT);
            loginAccountVo.setPassword(encodePassword);
            Account account = mapper.findAccount(loginAccountVo);
            if (account == null) return null;
            String token = JwtUtils.createJwtToken(account.getId(), account.getUsername());
            return new LoginResponseVo(token, account.getUsername(), JwtUtils.getJWTExpireTime(token));
        }
    }

    @Override
    public Integer resetPassword(ResetAccountVo resetAccountVo) {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            AccountMapper mapper = session.getMapper(AccountMapper.class);
            String encodePassword = BCrypt.hashpw(resetAccountVo.getOldPassword(), SALT);
            if (resetAccountVo.getUsername() == null || resetAccountVo.getOldPassword() == null || resetAccountVo.getNewPassword() == null)
                return 0;
            // 用户名密码校验
            Optional<Account> account = Optional.ofNullable(mapper.findAccount(new LoginAccountVo(resetAccountVo.getUsername(), encodePassword)));
            // 旧密码校验 ，校验失败则返回0
            if (!account.isPresent()) return 0;
            Account updateAccount = new Account();
            updateAccount.setUsername(resetAccountVo.getUsername());
            String updatePassword = BCrypt.hashpw(resetAccountVo.getNewPassword(), SALT);
            updateAccount.setPassword(updatePassword);
            return mapper.resetPassword(updateAccount);
        }

    }

    @Override
    public AccountVo getById(Integer userId) {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            AccountMapper mapper = session.getMapper(AccountMapper.class);
            Account account = null;
            account = mapper.findAccountById(userId);
            return new AccountVo(account.getId(),account.getUsername(), account.getEmail(), account.getRegisterTime(),account.getAvatar());
        }
    }

    @Override
    public boolean updateAccount(Integer userId, String email,String username) {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            AccountMapper mapper = session.getMapper(AccountMapper.class);
            Account account = new Account();
            account.setUsername(username);
            account.setEmail(email);
            account.setId(userId);
            Integer i = mapper.updateAccount(account);
            System.out.println(email);
            return i > 0;
        }
    }
}